﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Configuration;
using HF.Framework.SqlDataContract;

namespace HF.Framework.DataClient
{
    public  class DataProvider:IDatabase
    {
        #region Fields
        private DataClientType _dataClientType;
        private IDatabase _dataBase;
        /// <summary>
        /// 数据库类型,支持Sql Server, Oracle,Access等类型,还可以继续扩展支持的类型 .
        /// </summary>
        public DataClientType DataClientType
        {
            get { return _dataClientType; }
            set { _dataClientType = value; }

        }
       
        /// <summary>
        ///IDatabase接口 
        /// </summary>
        public IDatabase Database
        {
            get { return _dataBase; }
            set { _dataBase = value; }
        }
        #endregion

        #region Constructors
        /// <summary>
        /// 支持多数据库的构造函数
        /// </summary>
        public DataProvider()
        {
            string clientType =ConfigurationSettings.AppSettings["DataClientType"];
            _dataClientType = (DataClientType)Enum.Parse(typeof(DataClientType), clientType);
            switch (_dataClientType)
            {
                case DataClientType.SQLSERVER:
                    {
                        _dataBase = new SqlDatabase();

                        break;
                    }
                case DataClientType.ORACLE:
                    {
                       //  _dataBase = new OracleDatabase();
                        break;
                    }
                case DataClientType.ACCESS:
                    {
                        // _dataBase = new AccessDatabase();
                        break;
                    }
                default:
                    {
                        _dataBase = new SqlDatabase();

                        break;
                    }
            } 
        }
        
        #endregion

        #region 获得一个空结构
        public DataTable GetBlankTable(string tableName)
        {

            return GetBlankTable(tableName, null);
        }
        public DataSet GetBlankDataSet(List<string> tableNames)
        {
            return GetBlankDataSet(tableNames, null);
        }
        public DataTable GetBlankTable(string tableName, string databaseName)
        {
           return _dataBase.GetBlankTable(tableName,databaseName);
        }
        public DataSet GetBlankDataSet(List<string> tableNames, string databaseName)
        {
            return _dataBase.GetBlankDataSet(tableNames,databaseName);
        }
        #endregion

        #region sqlDataItem方式
        /// <summary>
        /// 执行一条sql语句或者存储过程,返回影响的条数 .
        /// 以参数方法访问数据库 
        /// </summary>
        /// <param name="sqlItem">SqlDataItem参数包含commandtext commandtype prameters信息</param>
        /// <returns>返回影响的数据条数</returns>
        public int ExecuteNonQuery(SqlDataItem sqlItem)
        {
            return _dataBase.ExecuteNonQuery(sqlItem);
        }

        /// <summary>
        ///执行一组sql语句或者存储过程,提供数据库事务支持。 
        /// 以参数方式访问数据库. 
        /// </summary>
        /// <param name="arySqlDataItem">SqlDataItemArray 包含一个或多个sqldataitem类型的参数信息</param>
        /// <returns>返回执行结果true成功, false失败.</returns>
        public bool ExecuteNonQueryArray(SqlDataItemArray arySqlDataItem)
        {
            return _dataBase.ExecuteNonQueryArray(arySqlDataItem);
        }
        /// <summary>
        /// 执行一条sql语句或者存储过程，返回一个数据集
        /// 以参数方法访问数据库. 
        /// </summary>
        /// <param name="sqlItem">SqlDataItem参数包含commandtext commandtype prameters信息</param>
        /// <returns>返回数据集</returns>
        public DataSet ExecuteQuery(SqlDataItem sqlItem)
        {
            return _dataBase.ExecuteQuery(sqlItem);
        }
        /// <summary>
        /// 执行一条sql语句或者存储过程,返回一个数据表 
        /// 以参数方法访问数据库.  
        /// </summary>
        /// <param name="sqlDataItem">SqlDataItem参数包含commandtext commandtype prameters信息</param>
        /// <returns>返回数据表</returns>
        public DataTable ExecuteDataTable(SqlDataItem sqlDataItem)
        {
            return _dataBase.ExecuteDataTable(sqlDataItem);
        }
        /// <summary>
        ///  执行一条sql语句或者存储过程,判断符合条的记录是否存在.
        ///  以参数方式访问数据库
        /// </summary>
        /// <param name="sqlItem">SqlDataItem参数包含commandtext commandtype prameters信息</param>
        /// <returns>返回true或者false</returns>
        public bool RecordExists(SqlDataItem sqlItem)
        {
            return _dataBase.RecordExists(sqlItem);

        }
        /// <summary>
        /// 执行一条sql语句或者存储过程,返回第一条数据记录的第一个字段的值 
        /// 以参数方法访问数据库.  
        /// </summary>
        /// <param name="sqlDataItem">SqlDataItem参数包含commandtext commandtype prameters信息</param>
        /// <returns>返回一个object类型的值</returns>
        public string ExecuteScalar(SqlDataItem sqlDataItem)
        {
            return _dataBase.ExecuteScalar(sqlDataItem);
        }
       
        #endregion

        #region 更新数据集
        public void UpdateDataSetWithTranse(DataSet ds)
        {
            UpdateDataSetWithTranse(ds, null);
        }
        public void UpdateDataTableWithTranse(DataTable dataTable)
        {
            UpdateDataTableWithTranse(dataTable, null);
        }
        /// <summary>
        /// 更新数据集，启动数据库事务
        /// </summary>
        /// <param name="ds">数据集</param>
        /// <param name="databaseName">数据库名</param>
        public void UpdateDataSetWithTranse(DataSet dataSet, string databaseName)
        {
            _dataBase.UpdateDataSetWithTranse(dataSet, databaseName);
        }
        /// <summary>
        /// 更新表,启动数据库事务
        /// </summary>
        /// <param name="dataTable">要更新的表</param>
        /// <param name="databaseName">数据库名</param>
        public void UpdateDataTableWithTranse(DataTable dataTable, string databaseName)
        {
            _dataBase.UpdateDataTableWithTranse(dataTable, databaseName);
        }


        #endregion

        #region 访问当前数据库中的表和字段
        public string GetCurrentDatabaseName()
        {
          return  _dataBase.GetCurrentDatabaseName();
        }
        public DataSet GetDBQuery(string databaseName)
        {
          return  _dataBase.GetDBQuery(databaseName);
        }
        public DataTable GetDBDataTable(string databaseName)
        {
            return _dataBase.GetDBDataTable(databaseName);
        }
        public DataSet GetDTColumnsQuery(string tableName, string databaseName)
        {
            return _dataBase.GetDTColumnsQuery(tableName, databaseName);
        }
        public DataTable GetDTColumnsDataTable(string tableName, string databaseName)
        {
            return _dataBase.GetDTColumnsDataTable(tableName, databaseName);
        }
        public string GetDBDateTime()
        {
            return _dataBase.GetDBDateTime();
        }
        #endregion
    }
    #region Enums
    public enum DataClientType
    {
        SQLSERVER,
        ORACLE,
        ACCESS
    }
    #endregion
}
